package com.li.spark.share

import org.apache.spark.{SparkConf, SparkContext}

/**
 * 多个节点对一个变量共享性操作。累加
 */
object AccumulatorOp {
  def main(args: Array[String]): Unit = {
    val sc: SparkContext = getSparkContext

    val RDD = sc.parallelize(Array(1, 2, 3, 4, 5), 2)
    //定义累加变量
    val sumAccumulator = sc.longAccumulator
    RDD.foreach(num => sumAccumulator.add(num))
    println(sumAccumulator.value)
    sc.stop();
  }


  private def getSparkContext = {
    //创建SparkContext
    val conf = new SparkConf();
    conf.setAppName("AccumulatorOp").setMaster("local");
    new SparkContext(conf);
  }
}
